package com.choosefine.it.basedata.service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import com.choosefine.it.base.dto.PageDTO;
import com.choosefine.it.basedata.dao.CompanyInfoMapper;
import com.choosefine.it.basedata.model.CompanyInfo;
import com.choosefine.it.basedata.model.vo.AffiliatedUnitVO;
import com.choosefine.it.basedata.model.vo.CompanyInfoFindVO;
import com.choosefine.it.basedata.model.vo.CompanyNameFindVO;
import com.choosefine.it.common.def.CommonConstant;
import com.choosefine.it.common.utils.DateUtils;
import com.choosefine.it.company.dao.CompanyUnitTypeMapper;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;

/**
 * 公司详情service
 * @author guwenwei
 *
 */
@Service("companyinfoService")
public class CompanyInfoService{
	@Resource
	CompanyInfoMapper companyinfomapper;
	
	@Resource
	CompanyUnitTypeMapper companyunittypemapper;
	
//	@Autowired
//	MongoTemplate mongoTemplate;
	
	public int deleteByPrimaryKey(Integer id) {
		return companyinfomapper.deleteByPrimaryKey(id);
	}

	public @interface FindByUserCode{};
	public CompanyInfo findByUserCode(String userCode) {
		return companyinfomapper.findByUserCode(userCode);
	}
	
	public String findHeadImageByUserCode(String userCode,boolean isLarge) {
		StringBuilder headImageStr=new StringBuilder();
		String headImage= companyinfomapper.findHeadImageByUserCode(userCode);
		if(isLarge&&StringUtils.isNotEmpty(headImage)){
			headImageStr.append(headImage).append(CommonConstant.IMAGE_LARGE_POSTFIX);
			return headImageStr.toString();
		}
		return  headImage;
	}

	public int deleteByUserCode(String userCodes) {
		int result = 0;
		String []idsStr=userCodes.split(",");
		for(int i=0;i<idsStr.length;i++){
			if (idsStr[i] != null) {
				Map<String, Object> params = new HashMap<>();
				params.put("isValid", CommonConstant.INVALID);
				params.put("userCode", idsStr[i]);
				params.put("utime", DateUtils.getNowTime());
				result+=companyinfomapper.deleteByUserCode(params);
			}
		}
		return result;
	}

	public List<CompanyInfo> selectCompanyInfoBy(Map<String, Object> map, PageDTO pageDTO) {
		PageHelper.startPage(pageDTO.getPageNum(), pageDTO.getPageSize());
		PageHelper.orderBy(pageDTO.getOrderBy());
		return companyinfomapper.selectCompanyInfoBy(map);
	}
	
	public List<CompanyInfo> selectAll() {
		
		return companyinfomapper.selectAll();
	}


	public int insert(CompanyInfo record) {
		int result = companyinfomapper.insert(record);
		//mongo写入公司信息数据
//		if(result>0){
//			HotCompany hotCompany = new HotCompany();
//			hotCompany.setCompanyName(record.getCompanyName());
//			hotCompany.setCtime(record.getCtime());
//			hotCompany.setUserCode(record.getUserCode());
//			hotCompany.setId(record.getId());
//			hotCompany.setIsValid(record.getIsValid());
//			hotCompany.setHot(ConstantDef.ISNOTHOT);
//			hotCompany.setOperator(record.getOperator());
//			hotCompany.setUseStatus(record.getUseStatus());
//			hotCompany.setCompanyArea(record.getCompanyArea());
//			hotCompany.setCompanyLegal(record.getCompanyLegal());
//			hotCompany.setCompanyType(record.getCompanyType());
//			hotCompany.setEnterpriseType(record.getEnterpriseType());
//			mongoTemplate.insert(hotCompany);
//		}
		return result;
	}
	
	public String getUnitType(Integer typeId){
		return companyunittypemapper.findUnitTypeById(typeId);
	}
	
	public String getCompanyType(Integer typeId){
		return companyunittypemapper.findRoleTypeById(typeId);
	}

	public int update(CompanyInfo record) {
		
		return companyinfomapper.updateByPrimaryKey(record);
	}

	public Page<CompanyInfoFindVO> findCompanyByRole(Map<String, Object>params,PageDTO pageDTO){
		PageHelper.startPage(pageDTO.getPageNum(), pageDTO.getPageSize());
		return companyinfomapper.findCompanyByRole(params);
	}

	public CompanyInfo selectCompanyInfoById(Integer id){
		return companyinfomapper.selectById(id);
	}
	
	public String selectUseCodeByName(String companyName){
		return companyinfomapper.selectUseCodeByName(companyName);
	}

	public Page<CompanyNameFindVO> findCompanyByUserCode(String userCode) {
		return companyinfomapper.findCompanyByUserCode(userCode);
	}

	public Page<CompanyNameFindVO> findCompanyByManagerUserCode(String managerUserCode) {
		return companyinfomapper.findCompanyByManagerUserCode(managerUserCode);
	}

	public Page<AffiliatedUnitVO> findAffiliatedUnits(PageDTO pageDTO, String userCode) {
		PageHelper.startPage(pageDTO.getPageNum(), pageDTO.getPageSize());
		return companyinfomapper.findAffiliatedUnits(userCode);
	}
}
